package cn.house.rent.service.impl;

import cn.house.rent.entity.User;
import cn.house.rent.mapper.HouseMapper;
import cn.house.rent.mapper.OrderMapper;
import cn.house.rent.service.OrderService;
import cn.house.rent.utils.Result;
import cn.house.rent.vo.OrderVo;
import cn.house.rent.vo.TenantHomeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private HouseMapper houseMapper;

    //租客个人订单模块查询和搜索查询
    @Override
    public Result tenantOrder(User user, OrderVo orderVo) {
        PageHelper.startPage(orderVo.getPage(), orderVo.getLimit());
        List<OrderVo> orderVos = orderMapper.selectTenantOrder(user, orderVo);
        for (OrderVo o : orderVos) {
            if (o.getHouseName() == null) {
                o.setHouseName("已下线");
            }
        }
        Result result = new Result();
        PageInfo<OrderVo> page = new PageInfo<>(orderVos);
        if (orderVos != null) {
            result.setCode(0);
            result.setMsg("查询成功！");
            result.setCount(page.getTotal());
            result.setData(orderVos);
        } else {
            result.setCode(500);
            result.setMsg("查询失败！");
        }
        return result;
    }

    //房东个人订单模块查询和搜索查询
    @Override
    public Result landOrder(User user, OrderVo orderVo) {
        PageHelper.startPage(orderVo.getPage(), orderVo.getLimit());
        List<OrderVo> orderVos = orderMapper.selectLandOrder(user, orderVo);
        for (OrderVo o : orderVos) {
            if (o.getHouseName() == null) {
                o.setHouseName("已下线");
            }
        }
        Result result = new Result();
        PageInfo<OrderVo> page = new PageInfo<>(orderVos);
        if (orderVos != null) {
            result.setCode(0);
            result.setMsg("查询成功！");
            result.setCount(page.getTotal());
            result.setData(orderVos);
        } else {
            result.setCode(500);
            result.setMsg("查询失败！");
        }
        return result;
    }

    //租客我的家  取消功能
    @Override
    public Result cancelTenant(TenantHomeVo tenantHomeVo) {
        //状态为预定 就删除两表订单  并将房子状态该为出租
        if (tenantHomeVo.getHouseStatus() == 3) {
            tenantHomeVo.setUpdateHouseStatus(7);
            int tenLine = orderMapper.deleteTenantOrderById(tenantHomeVo.getId());
            int lardLine = orderMapper.deleteLandOrderById(tenantHomeVo.getId());
            int houseLine = houseMapper.updateHouseStatusById(tenantHomeVo);
            if (tenLine > 0 && lardLine > 0 && houseLine > 0) {
                return Result.builder().code(0).msg("取消成功！").build();
            } else {
                return Result.builder().code(500).msg("取消失败！").build();
            }
        } else if (tenantHomeVo.getHouseStatus() == 6) {
            //状态为申请退租  就将房子状态该为已租
            tenantHomeVo.setUpdateHouseStatus(4);
            int line = houseMapper.updateHouseStatusById(tenantHomeVo);
            if (line > 0) {
                return Result.builder().code(0).msg("取消成功！").build();
            } else {
                return Result.builder().code(500).msg("取消失败！").build();
            }
        }
        return null;
    }

    //租客个人订单  取消功能
    @Override
    public Result cancelTenOrder(TenantHomeVo tenantHomeVo) {
        //要删除租客和房东订单表对应的订单  并修改房屋状态为正在出租
        tenantHomeVo.setUpdateHouseStatus(7);
        int tenLine = orderMapper.deleteTenantOrderById(tenantHomeVo.getId());
        int landLine = orderMapper.deleteLandOrderById(tenantHomeVo.getId());
        int houseLine = houseMapper.updateHouseStatusById(tenantHomeVo);
        if (tenLine > 0 && landLine > 0 && houseLine > 0) {
            return Result.builder().code(0).msg("取消成功！").build();
        } else {
            return Result.builder().code(500).msg("取消失败！").build();
        }
    }

    //租客个人订单  删除功能
    @Override
    public Result delTenOrder(TenantHomeVo tenantHomeVo) {
        //只删除租客订单表的对应订单
        int line = orderMapper.deleteTenantOrderById(tenantHomeVo.getId());
        if (line > 0) {
            return Result.builder().code(0).msg("删除成功！").build();
        } else {
            return Result.builder().code(500).msg("删除失败！").build();
        }
    }

    //房东  个人订单  同意功能
    @Override
    public Result passOrder(OrderVo orderVo) {
        //同意订单  将房屋状态改为已租赁  订单状态改为交易成功
        orderVo.setUpdateOrderStatus(1);
        int landLine = orderMapper.updateLandOrder(orderVo);
        int tenLine = orderMapper.updateTenOrder(orderVo);
        TenantHomeVo tenantHomeVo = TenantHomeVo.builder().houseId(orderVo.getHouseId()).updateHouseStatus(4).build();
        int houseLine = houseMapper.updateHouseStatusById(tenantHomeVo);
        if (landLine > 0 && tenLine > 0 && houseLine > 0) {
            return Result.builder().code(0).msg("处理成功！").build();
        } else {
            return Result.builder().code(500).msg("处理失败！").build();
        }
    }

    //房东  个人订单  驳回功能
    @Override
    public Result rejectOrder(OrderVo orderVo) {
        //驳回订单  将房屋状态改为未租赁  订单状态改为交易失败
        orderVo.setUpdateOrderStatus(2);
        int landLine = orderMapper.updateLandOrder(orderVo);
        int tenLine = orderMapper.updateTenOrder(orderVo);
        TenantHomeVo tenantHomeVo = TenantHomeVo.builder().houseId(orderVo.getHouseId()).updateHouseStatus(7).build();
        int houseLine = houseMapper.updateHouseStatusById(tenantHomeVo);
        if (landLine > 0 && tenLine > 0 && houseLine > 0) {
            return Result.builder().code(0).msg("处理成功！").build();
        } else {
            return Result.builder().code(500).msg("处理失败！").build();
        }
    }

    //房东  个人订单  删除功能
    @Override
    public Result delLandOrder(OrderVo orderVo) {
        //只删除房东订单表的对应订单
        int line = orderMapper.deleteLandOrderById(orderVo.getId());
        if (line > 0) {
            return Result.builder().code(0).msg("删除成功！").build();
        } else {
            return Result.builder().code(500).msg("删除失败！").build();
        }
    }

    //添加订单
    @Override
    public Result addOrder(OrderVo orderVo) {
        int tenantOrder = orderMapper.addTenantOrder(orderVo);
        int landOrder = orderMapper.addLandOrder(orderVo);
        TenantHomeVo tenantHomeVo = TenantHomeVo.builder().houseId(orderVo.getHouseId()).updateHouseStatus(3).build();
        int i = houseMapper.updateHouseStatusById(tenantHomeVo);
        Result result = Result.builder().build();
        if (tenantOrder > 0 && landOrder > 0 && i > 0) {
            result.setCode(0);
            result.setMsg("预定成功！");
        } else {
            result.setCode(500);
            result.setMsg("预定失败！");
        }
        return result;
    }
}
